//
//  MYXOpenLuckyMoneyView.m
//  meiyinxiang
//
//  Created by apple on 2017/10/30.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "MYXOpenLuckyMoneyView.h"
#import <POP.h>
#import <SDImageCache.h>
#import "MYXRecieveMoneyListViewModel.h"
#import "UIImage+Category.h"

#import "MYXLukcyMoneyDetailViewController.h"
@interface MYXOpenLuckyMoneyView ()
@property (weak, nonatomic) IBOutlet UIView *backgroundView;
@property (weak, nonatomic) IBOutlet UIImageView *hongbaoImageView;
@property (weak, nonatomic) IBOutlet UIImageView *xiangImageView;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UIButton *openBtn;
    @property (weak, nonatomic) IBOutlet UIView *hongbaoBgview;
    @property (weak, nonatomic) IBOutlet UILabel *luckyMoneyOwnerName;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLayoutCons;

@end
@implementation MYXOpenLuckyMoneyView
+ (MYXOpenLuckyMoneyView *)loadNibMYXLuckyMoneyView{
    UINib *nib = [UINib nibWithNibName:@"MYXOpenLuckyMoneyView" bundle:[NSBundle mainBundle]];
    return [[nib instantiateWithOwner:nil options:nil] lastObject];
}
- (void)awakeFromNib{
    [super awakeFromNib];
    //self.iconImageView.image = [UIImage imageNamed:@"80"];
    //SDWebImage 一个难以想象的BUG
    if (iphone5S) {
        self.topLayoutCons.constant = 30;
    }
    [self fixSDBug];
    self.hongbaoBgview.layer.cornerRadius = 15;
    self.hongbaoBgview.layer.masksToBounds = YES;
    //添加点击红包
    [self.openBtn addTarget:self action:@selector(openLuckyMoney) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - PrivateMethod
- (void)openLuckyMoney{
    [self tapMiss];
    [SVProgressHUD setMinimumDismissTimeInterval:1.0];
    [SVProgressHUD showWithStatus:@"加载中..."];
    __weak typeof(self) weakself = self;
    [self.model getMoneyDetailWithIsPullup:NO Completion:^(BOOL success, NSString *message) {
        [SVProgressHUD dismiss];
        if (success) {
            MYXLukcyMoneyDetailViewController *detailVC = [[MYXLukcyMoneyDetailViewController alloc] init];
          //  detailVC.detailviewModel = weakself.model.viewModel;
            detailVC.model = weakself.model;
            [self.mViewController.navigationController pushViewController:detailVC animated: YES];
            
        }
    }];
//    [self.model getMoneyDetailWithCompletion:^(BOOL success, NSString *message) {
//
//
//
//    }];

}
- (void)addTapGes{
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMiss)];
    self.backgroundView.userInteractionEnabled = YES;
    [self.backgroundView addGestureRecognizer:tapGes];
    self.hongbaoImageView.userInteractionEnabled = YES;
    self.xiangImageView.userInteractionEnabled = YES;
    
}
- (void)tapMiss{
    //缩放动画
    POPBasicAnimation *scaleAnim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY];
    scaleAnim.fromValue = [NSValue valueWithCGPoint:CGPointMake(1, 1)];
    scaleAnim.toValue = [NSValue valueWithCGPoint:CGPointMake(0.1, 0.1)];
    scaleAnim.duration = 0.5;
    [self.hongbaoBgview pop_addAnimation:scaleAnim forKey:nil];
    scaleAnim.completionBlock = ^(POPAnimation *anim, BOOL finished) {
        
        self.hidden = YES;
    };
}
//添加红包动画
- (void)addLuckyMoneyAnnamation{
    //设置数据源
    if (self.model) {
        //添加数据源
        [self configData];
        //添加动画
        [self addPopAnimation];
}
   
}
- (void)configData{
    self.luckyMoneyOwnerName.text = [NSString stringWithFormat:@"%@的红包",self.model.username];
    self.iconImageView.layer.cornerRadius = 33;
    self.iconImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.iconImageView.layer.masksToBounds = YES;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:self.model.userface] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image) {
//            self.iconImageView.image = [UIImage hbj_AvatarIcon:image size:image.size backgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"hongbaobgImg"]]];
            self.iconImageView.image = image;
        }else{
            self.iconImageView.image = [UIImage imageNamed:@"AppIcon"];
        }
        
    }];
}
- (void)fixSDBug{
    NSString *userAgent = @"";
    userAgent = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleExecutableKey] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleIdentifierKey], [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleVersionKey], [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], [[UIScreen mainScreen] scale]];
    
    if (userAgent) {
        if (![userAgent canBeConvertedToEncoding:NSASCIIStringEncoding]) {
            NSMutableString *mutableUserAgent = [userAgent mutableCopy];
            if (CFStringTransform((__bridge CFMutableStringRef)(mutableUserAgent), NULL, (__bridge CFStringRef)@"Any-Latin; Latin-ASCII; [:^ASCII:] Remove", false)) {
                userAgent = mutableUserAgent;
            }
        }
        [[SDWebImageDownloader sharedDownloader] setValue:userAgent forHTTPHeaderField:@"User-Agent"];
    }
}
- (void)addPopAnimation{
    /*
     // 1> 创建动画
     let anim: POPSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
     
     // 2> 设置动画属性
     anim.fromValue = btn.center.y + 350
     anim.toValue = btn.center.y
     
     // 弹力系数，取值范围 0~20，数值越大，弹性越大，默认数值为4
     anim.springBounciness = 8
     // 弹力速度，取值范围 0~20，数值越大，速度越快，默认数值为12
     anim.springSpeed = 8
     
     // 设置动画启动时间
     anim.beginTime = CACurrentMediaTime() + CFTimeInterval(i) * 0.5
     
     // 3> 添加动画
     btn.pop_add(anim, forKey: nil)
     */
    self.hidden = NO;
    self.center = self.mSuperView.center;
    self.bounds = self.mSuperView.bounds;
    //缩放动画
    POPBasicAnimation *scaleAnim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY];
    scaleAnim.fromValue = [NSValue valueWithCGPoint:CGPointMake(0.1, 0.1)];
    scaleAnim.toValue = [NSValue valueWithCGPoint:CGPointMake(1, 1)];
    scaleAnim.duration = 0.5;
    WS(weakSelf);
    [self.hongbaoBgview pop_addAnimation:scaleAnim forKey:nil];
    scaleAnim.completionBlock = ^(POPAnimation *anim, BOOL finished) {
        POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY];
        // 2> 设置动画属性
        springAnimation.fromValue = @(CGPointMake(1.05, 1.05));
        springAnimation.toValue = @(CGPointMake(1.0, 1.0));
        springAnimation.springBounciness = 3;
        springAnimation.springSpeed = 3;
        springAnimation.beginTime = CACurrentMediaTime();
        [weakSelf.hongbaoBgview pop_addAnimation:springAnimation forKey:nil];
    
    };
    [self addTapGes];
}
- (void)removeFromSuperview{
    [super removeFromSuperview];
    //[self removeFromSuperview];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
